home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
Issue55
/
construc
/
BASE64.PAS
next >
Wrap
Pascal/Delphi Source File
|
2000-02-07
|
2KB
|
59 lines
unit Base64;
interface
function UnBase64(Str: String): String;
implementation
function UnBase64(Str: String): String;
type
TTriplet = Array[0..2] of Byte;
TKwartet = Array[0..3] of Byte;
var
Kwartet: TKwartet;
Triplet: TTripLet;
procedure Kwartet2Triplet(Kwartet: TKwartet; var Triplet: TTriplet);
var
i: Integer;
begin
for i:=0 to 3 do
begin
case Chr(Kwartet[i]) of
'A'..'Z': Kwartet[i] := 0 + Kwartet[i] - Ord('A') + 32;
'a'..'z': Kwartet[i] := 26 + Kwartet[i] - Ord('a') + 32;
'0'..'9': Kwartet[i] := 52 + Kwartet[i] - Ord('0') + 32;
'+': Kwartet[i] := 62 + 32;
'/': Kwartet[i] := 63 + 32;
end
end;
Triplet[0] := ((Kwartet[0] - 32) SHL 2) +
(((Kwartet[1] - 32) AND $30) SHR 4);
Triplet[1] := (((Kwartet[1] - 32) AND $0F) SHL 4) +
(((Kwartet[2] - 32) AND $3C) SHR 2);
Triplet[2] := (((Kwartet[2] - 32) AND $03) SHL 6) +
((Kwartet[3] - 32) AND $3F)
end {Kwartet2Triplet};
var
i: Integer;
begin
Result := '';
while Length(Str) > 4 do
begin
for i:=1 to 4 do
Kwartet[Pred(i)] := Ord(Str[i]);
Delete(Str,1,4);
Kwartet2Triplet(Kwartet,Triplet);
for i:=0 to 2 do Result := Result + Chr(Triplet[i]);
end;
for i:=1 to 4 do
if i <= Length(Str) then
Kwartet[Pred(i)] := Ord(Str[i])
else Kwartet[Pred(i)] := Ord('/');
Kwartet2Triplet(Kwartet,Triplet);
for i:=0 to 2 do Result := Result + Chr(Triplet[i])
end {Unbase64};
end.